<template>
  <a-button shape="round" style="margin-right: 5px" @click="copy" icon="copy">复制</a-button>
</template>
<script>
import {createProps, guid} from '@/utils';

export default {
  name: 'CopyButton',
  props: {
    config: createProps(Object, () => undefined),
    context: createProps(Object, () => undefined),
    dataSource: createProps(Array, () => []),
    subFormDefinition: createProps(Object),
    selectedRowId: createProps(String),
  },
  methods: {
    copy() {
      let row = this.dataSource.find(item => item.id === this.selectedRowId);
      if (!row) {
        this.$message.warning('请选择一行复制');
        return;
      }
      row = JSON.parse(JSON.stringify(row));
      row.id = guid();
      this.dataSource.push(row);

      row._isNew = true;
      this.$nextTick(() => {
        this.$emit('updateSubFormData')
      })
    },
  }
};
</script>